package com.area.cth.controller.admin;

import com.area.cth.common.Result;
import com.area.cth.pojo.Goods;
import com.area.cth.service.GoodsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@Tag(name = "管理端商品相关接口")
@RestController("adminGoodsController")
@RequestMapping("/admin/goods")
@CrossOrigin
@Slf4j
public class GoodsController {
    @Autowired
    private GoodsService gs;

    @Operation(summary = "分页查询")
    @GetMapping("/list")
    public Result<IPage<Goods>> getGoodsList(Integer pageNo, Integer pageSize,@RequestParam(required = false) Integer auditState, @RequestParam(required = false) Integer categoryId, @RequestParam(required = false) String keyword) {
        IPage<Goods> goodsList = gs.goodsList(pageNo, pageSize, auditState,categoryId, keyword);
        return Result.success(goodsList);
    }

    @Operation(summary = "根据id查询商品")
    @GetMapping("/getById")
    public Result<Goods> goodsById(Integer id) {
        Goods goodsById = gs.getGoodsById(id);
        return Result.success(goodsById);
    }

    @Operation(summary = "删除商品")
    @DeleteMapping
    @CacheEvict(cacheNames = "goodsCache", allEntries = true)
    public Result deleteGoods(Integer id) {
        gs.deleteGoods(id);
        return new Result(0, "下架成功", null);
    }

    @Operation(summary = "修改商品审核状态")
    @PutMapping("/updateState")
    @CacheEvict(cacheNames = "goodsCache", allEntries = true)
    public Result updateState(Integer goodsId, Integer state) {
        gs.lambdaUpdate()
                .set(Goods::getAuditState, state)
                .eq(Goods::getId, goodsId)
                .update();
        return Result.success();
    }
}
